package com.dobest.securitydemo3.domain.vo;

import com.dobest.securitydemo3.domain.Role;
import lombok.Data;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

/**
 * Copyright © 2018胡建松. All rights reserved.
 *
 * @Package: com.dobest.securitydemo3.domain.vo
 * @author: hujiansong
 * @date: 2018/7/20 15:57
 * @version: V1.0
 */
@Data
public class UserVo implements UserDetails {

    private String username;

    private String password;

    private List<Role> roles;

    private static final String ROLE_PREFIX = "ROLE_";

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<Role> roles = this.getRoles();
        List<SimpleGrantedAuthority> grantedAuthorities = new ArrayList<>();
        if (roles.size() > 0) {
            roles.forEach(role -> grantedAuthorities.add(new SimpleGrantedAuthority(ROLE_PREFIX + role.getRoleName().toUpperCase())));
        }
        return grantedAuthorities;
    }

    @Override
    public String getPassword() {
        return password;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return true;
    }

    @Override
    public boolean isAccountNonLocked() {
        return true;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return true;
    }

    @Override
    public boolean isEnabled() {
        return true;
    }
}
